import {Component, OnInit} from '@angular/core';
import {SysPermissions, SysPermissionsCondition} from '../../sys-permissions.type';
import {ActivatedRoute, Router} from '@angular/router';
import {Services} from '../../../../../frame/module/services';
import {ErrRes} from '../../../../../frame/global/err-res';
import {isNullOrUndefined} from 'util';
import {UrlRou} from '../../../../../frame';
import {ModifySysPermissionsComponent} from '../modify-sys-permissions.component';
import {SysMenuCondition} from '../../../sys-menu/sys-menu.type';

@Component({
    selector: 'app-main-modify-sys-permissions',
    templateUrl: './main-modify-sys-permissions.component.html',
    styleUrls: ['./main-modify-sys-permissions.component.css']
})
export class MainModifySysPermissionsComponent implements OnInit {

    errRes = new ErrRes();

    domain: SysPermissions = new SysPermissions();
    menuDomains = [];
    extend = {
        domain: {},
    };

    func = {
        loadDomain: (uuid: string) => {
            const condition: SysPermissionsCondition = new SysPermissionsCondition();
            condition.uuid = uuid;
            Services.sysPermissions.info(condition).subscribe(item => {
                if (!isNullOrUndefined(item) && !isNullOrUndefined(item.domain)) {
                    this.domain = item.domain;
                }
            });
        },
        update: {
            req: (event) => {
                Services.sysPermissions.update(this.domain, this.func.update.callback).subscribe();
            },
            callback: {
                success: (result) => {
                    this.router.navigate([UrlRou.sysMenu.list, {
                        moduleId: 'system-management',
                        menuId: 'sys-permissions'
                    }]);
                },
                error: (result) => {
                    this.errRes.error = result.error;
                    this.errRes.statusText = result.statusText;
                }
            }
        }
    };

    initData = () => {
        const sysMenu = new SysMenuCondition();
        Services.sysMenu.list(sysMenu, {
            page: 0,
            size: 2000,
            orders: 'serNum-asc'
        }).subscribe(item => {
            this.menuDomains = item.page.content;
        });
    };

    constructor(public modifySysPermissions: ModifySysPermissionsComponent, private router: Router, private route: ActivatedRoute) {
    }

    ngOnInit() {
        this.modifySysPermissions.main = this;
        this.initData();
        const uuid = this.route.snapshot.paramMap.get('uuid');
        this.func.loadDomain(uuid);
    }

}
